﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 赵国伟
  //文件功能描述：支付方式管理
  //创建人：赵国伟
  //生成时间：2012/04/21 16:15:00
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Shopping.Common;
using Shopping.Model;
using Shopping.DAL;

namespace Shopping.BLL
{
    public class PayModesManage
    {
        private readonly DALPayModes dalPayMode = new DALPayModes();

        #region "查询一条支付方式信息； by 赵国伟 2012-04-21"
        /// <summary>
        /// 查询一条支付方式信息；
        /// </summary>
        /// <param name="payModeId"></param>
        /// <returns></returns>
        public PayModes GetPayModeInfo(int payModeId)
        {
            return dalPayMode.GetModel(payModeId);
        }
        #endregion

        #region "删除一条支付方式信息；by 赵国伟 2012-04-21"
        /// <summary>
        /// 删除一条支付方式信息；
        /// </summary>
        /// <param name="payModeId"></param>
        /// <returns></returns>
        public ReturnValue DeletePayMode(int payModeId)
        {
            ReturnValue ret = new ReturnValue();
            if (dalPayMode.Delete(payModeId) > 0)
            {
                ret.Result = ResultType.Success;
                ret.Message = "删除成功！";
            }
            return ret;
        }
        #endregion

        #region "保存一条支付方式信息； by 赵国伟 2012-04-21"
        /// <summary>
        /// 保存一条支付方式信息；
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public ReturnValue SavePayMode(PayModes d)
        {
            ReturnValue ret = new ReturnValue();
            if (dalPayMode.QueryList(1, "PayModeName='" + d.PayModeName + "' and PayModeID <>" + d.PayModeID, "") != null)
            {
                ret.Message = "该支付方式已存在！请重新输入！";
                return ret;
            }
            d.CreateDate = DateTime.Now;
            d.LastModifyDate = DateTime.Now;

            if (d.PayModeID <= 0)
            {
                if (dalPayMode.Add(d) > 0)
                {
                    ret.Result = ResultType.Success;
                    ret.Message = "保存成功！";
                }
            }
            else
            {
                PayModes o = dalPayMode.GetModel(d.PayModeID);
                d.CreateDate = o.CreateDate;
                if (dalPayMode.Update(d) > 0)
                {
                    ret.Result = ResultType.Success;
                    ret.Message = "保存成功！";
                }
            }
            return ret;
        }
        #endregion

        #region "查询指定状态下的所有支付方式 by 赵国伟 2012-04-21"
        /// <summary>
        /// 查询指定状态下的所有支付方式
        /// </summary>
        /// <param name="IsAvailable"></param>
        /// <returns></returns>
        public List<PayModes> QueryALL(bool IsAvailable)
        {
            string cond = "1=1 ";
            if (IsAvailable)
                cond += " and Status='" + Status.正用.ToString() + "'";
            string orderby = "OrderValue, CreateDate desc ";
            return dalPayMode.QueryList(cond, orderby);
        }
        #endregion

        #region " 获取支付模式的下拉选项； by 赵国伟 2012-06-11"
        /// <summary>
        /// 获取支付模式的下拉选项；
        /// </summary>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string QueryPayModeToSelectOption(string defaultValue)
        {
            List<IDNameObj> datas = DALHelper.QueryIDNameObjs<PayModes>("", PayModesEnum.PayModeName);
            StringBuilder sb = new StringBuilder(500);
            if (datas != null)
            {
                foreach (IDNameObj datainfo in datas)
                {
                    sb.Append("<option value=\"");
                    sb.Append(datainfo.Name);
                    sb.Append("\" ");
                    if (defaultValue == datainfo.Name.ToString())
                        sb.Append(" Selected ");
                    sb.Append(">");
                    sb.Append(datainfo.Name);
                    sb.Append("</option>");
                }
            }
            return sb.ToString();
        }
        #endregion



    }
}
